package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.CatalogsAttention;
import cn.wolfcode.trip.base.domain.StrategyCatalog;
import cn.wolfcode.trip.base.mapper.CatalogsAttentionMapper;
import cn.wolfcode.trip.base.mapper.StrategyCatalogMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyCatalogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StrategyCatalogServiceImpl implements IStrategyCatalogService {
    @Autowired
    private StrategyCatalogMapper catalogMapper;

    @Autowired
    private CatalogsAttentionMapper catalogsAttentionMapper;

    @Override
    public PageInfo<StrategyCatalog> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<StrategyCatalog> list = catalogMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public void saveOrUpdate(StrategyCatalog catalog) {
        if (catalog.getSequence()==null) {
           int count = catalogMapper.selectMaxSequence(catalog.getStrategy().getId());
           count++;
           catalog.setSequence(count);
        }
        if (catalog.getId()==null) {
            catalogMapper.insert(catalog);
        }else{
            catalogMapper.updateByPrimaryKey(catalog);
        }
    }

    @Override
    public List<StrategyCatalog> selectCatalogByStrategyId(Long strategyId) {

        return catalogMapper.selectCatalogByStrategyId(strategyId);
    }

    @Override
    public List<StrategyCatalog> selectCatalogForLoop(Long strategyId) {
        return catalogMapper.selectCatalogForLoop(strategyId);
    }



    @Override
    public void deleteChose(Long catalogsId, Long userId) {
        catalogsAttentionMapper.deleteChose(catalogsId, userId);
    }

    @Override
    public void insertChose(Long catalogsId, Long userId) {
        catalogsAttentionMapper.insertChose(catalogsId, userId);
    }

    @Override
    public int selectLikeCountByCommentId(Long catalogsId) {
        int count = catalogsAttentionMapper.selectLikeCountByCommentId(catalogsId);
        return count;
    }

    @Override
    public CatalogsAttention selectByCatalogsIdAndUserId(Long catalogsId, Long userId) {
        CatalogsAttention catalogsAttention = catalogsAttentionMapper.selectByCatalogsIdAndUserId(catalogsId, userId);
        return catalogsAttention;
    }


}
